<template>
	<el-main>
		<el-row :gutter="24">
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="机构编码" tips="机构编码" groupSeparator cols :value1="String(agengtCode || '')" title1="机构编码">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="机构时长" tips="机构课时数数据统计" groupSeparator cols :value1="String(agentTotalHours || 0)" title1="机构时长">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="教练时长" tips="教练时长数据统计" groupSeparator cols :value1="String(teacherTotalHours || 0)" title1="教练时长">
						</statistic>
					</template>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="24">
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="30分钟课时" tips="课时数据统计" groupSeparator cols :value1="String(classHour30 || 0)" title1="30分钟课" :exchangeType="2" isButton @buttonFun="exchangeFun">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="60分钟课时" tips="课时数据统计" groupSeparator cols :value1="String(classHour60 || 0)" title1="60分钟课" :exchangeType="1" isButton @buttonFun="exchangeFun">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="用户数统计" tips="用户数据统计" groupSeparator cols :value1="String(user.total || 0)" title1="学员总数量">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<!-- <el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="体验课" tips="课时数据统计" groupSeparator cols :value1="trialCourse" title1="体验课">
						</statistic>
					</template>
				</el-card>
			</el-col> -->
		</el-row>
		<el-row :gutter="24">
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="机构数统计" tips="机构数据统计" groupSeparator cols :value1="String(agent.total || 0)" title1="机构总数量">
						</statistic>
					</template>
				</el-card>
			</el-col>
			<el-col :lg="8">
				<el-card class="box-card pr" shadow="never" style="height: 120px;">
					<template #header>
						<statistic title="教练数统计" tips="教练数据统计" groupSeparator cols :value1="String(teacher.total || 0)" title1="教练总数量">
						</statistic>
					</template>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="4" v-if="userInfo?.shareCode?.classHourCode">
			<el-col :lg="4">
				<el-card class="box-card pr" style="display: flex; align-items: center; justify-content: center;" shadow="never">
					<div style="display: flex; align-items: center; flex-direction: column; justify-content: center;">
							<el-image contain style="width: 165px; height: 174px" class="image" :src="userInfo?.shareCode?.classHourCode" fit="cover" :preview-src-list="[userInfo?.shareCode?.classHourCode]" hide-on-click-modal append-to-body :z-index="3500" v-if="userInfo?.shareCode?.classHourCode">
						</el-image>
						<div>分享二维码</div>
					</div>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="24" class="panel-group">
			<el-col :lg="12">
				<el-card class="box-card pr" shadow="never">
					<classHourLog :type="1" :mapTitle="'购入记录'"></classHourLog>
				</el-card>
			</el-col>
			<el-col :lg="12">
				<el-card class="box-card pr" shadow="never">
					<classHourLog :type="2" :mapTitle="'售出记录'"></classHourLog>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<exchange ref="exchange" v-if="dialog.exchange" :classHour30="classHour30" :classHour60="classHour60"  @success="exchangeSuccess"></exchange>
</template>

<script>
import statistic from './components/statisticV2.vue';
import classHourLog from './components/classHourLog.vue';
import exchange from './components/exchange.vue';
import {mapState} from 'vuex';

export default {
	name: 'GeneralIndex',
	components: {
		statistic,
		classHourLog,
		exchange
	},
	emits: ['on-mounted'],
	data() {
		return {
			dialog: {
				exchange: false,
			},
			user: { total: 0 },
			agent: { total: 0 },
			teacher: { total: 0 },
			agengtCode: '',
			agentTotalHours: 0, 
			teacherTotalHours: 0,
			classHour30: 0,
			classHour60: 0,
			trialCourse: 0
		}
	},
	computed: {
		...mapState({
			userInfo: state => state.viewTags.userInfo
		})
	},
	mounted() {
		this.$emit('on-mounted')
		this.getData()

		var userInfo = this.$TOOL.data.get("G_USER_INFO");
		this.agengtCode = userInfo.code;
		this.classHour30 = userInfo.classHour30
		this.classHour60 = userInfo.classHour60
		this.trialCourse = userInfo.trialCourse
		this.agentTotalHours = this.classHour30 + this.classHour60 + this.trialCourse
	},
	methods: {
		async getData() {
			this.loading = true
			await this.$API.statistics.home.get().then(res => {
				this.loading = false
				if (res.error == 0) {
					this.user.total = res.data.userNumbers
					this.agent.total = res.data.agentNumbers
					this.teacher.total = res.data.teacherNumbers
					this.teacherTotalHours = res.data.teacherTotalHours
				}
			}, () => {
				this.loading = false
			})
		},
		exchangeFun(type) {
			let title = ''
			if (type == 1) {
				if (this.classHour60 <= 0) {
					this.$notify.error('暂无可兑换的课时')
					return false;
				}
				title = '60分钟兑换30分钟课时'
			}else if (type == 2) {
				if (this.classHour30 <= 0) {
					this.$notify.error('暂无可兑换的课时')
					return false;
				}
				title = '30分钟兑换60分钟课时'
			}
			this.dialog.exchange = true
			this.$nextTick(() => {
				this.$refs.exchange.open(title, type)
			})
			return false
		},
		exchangeSuccess() {
			this.dialog.exchange = false
			this.$API.auth.user.get().then(user => {
				let userInfo = user.data
				this.$store.commit('userInfo', userInfo)
				this.$TOOL.data.set("G_USER_INFO", userInfo)

				this.classHour30 = userInfo.classHour30
				this.classHour60 = userInfo.classHour60
				this.trialCourse = userInfo.trialCourse
				this.agentTotalHours = this.classHour30 + this.classHour60 + this.trialCourse
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.sc-statistic-title {font-size: 12px;color: #999;margin-bottom: 30px;}
.sc-statistic-tips {margin-left: 5px;}
.sc-statistic-content {font-size: 12px;color: #333; display: flex;justify-content: center; align-items: center;}
.sc-statistic-content-value {font-weight: bold;}
.sc-statistic-content-prefix {margin-right: 5px;}
.sc-statistic-content-suffix {margin-left: 5px;font-size: 12px;}
.sc-statistic-description {margin-top: 10px;color: #999;}

.up {
	color: #F56C6C;
	margin-left: 5px;
}

.down {
	color: #67C23A;
	margin-left: 5px;
}

.flex-content {
	display: flex;
}

.paihang .items {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 15px;
}

.paihang .items .userimg {
	width: 30px;
	height: 30px;
	border-radius: 100%;
	margin-right: 10px;
}

.paihang .icon {
	width: 20px;
}

.paihang h2 {
	font-weight: 400;
	font-size: 12px;
}

.paihang .leftbox {
	display: flex;
	align-items: center;
}

.paihang .boxs {
	display: flex;
	align-items: center;
	justify-content: space-between;
	position: relative;
	text-align: center;
	margin-bottom: 20px;
	border-bottom: 1px solid #e6e6e6;
	padding: 20px 0;
	border-top: 1px solid #e6e6e6;
}

.paihang .boxs>div {
	flex: 1;
	position: relative;
}

.paihang .boxs>div:after {
	content: '';
	display: block;
	position: absolute;
	right: 0;
	top: 0;
	height: 100%;
	width: 1px;
	background: #e6e6e6;
}

.paihang .boxs>div:last-child::after {
	display: none;
}

::v-deep .study-card .el-card__body {
	padding: 10px 15px !important;
}

.other-info {
	border-top: 1px solid #e6e6e6;
	padding: 10px 20px;
	position: absolute;
	left: 0;
	bottom: 0;
	right: 0;
}

.card-body {
	padding: 9px 20px 15px;
}

.panel-group {
	margin-top: 0;

	.card-panel-col {
		margin-bottom: 20px;
		padding-right: 10px !important;
		padding-left: 10px !important;
	}

	.card-panel {
		height: 500px;
		cursor: pointer;
		font-size: 12px;
		position: relative;
		overflow: hidden;
		color: #666;
		background: #fff;
		box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
		border-color: rgba(0, 0, 0, 0.05);
		padding: 0px 20px;

		h3 {
			font-size: 16px;
			font-weight: 500;
			padding-top: 20px;
			display: inline-block;
		}

		&:hover {
			.card-panel-icon-wrapper {
				color: #fff;
			}

			.icon-people {
				background: #399afb;
			}

			.icon-message {
				background: #4c9fef;
			}

			.icon-money {
				background: #ff9f00;
			}

			.icon-shopping {
				background: #f9646a;
			}
		}

		.icon-people {
			color: #399afb;
		}

		.icon-message {
			color: #4c9fef;
		}

		.icon-money {
			color: #ff9f00;
		}

		.icon-shopping {
			color: #f9646a;
		}

		.card-panel-icon-wrapper {
			float: left;
			margin: 14px 0 0 14px;
			padding: 16px;
			transition: all 0.38s ease-out;
			border-radius: 6px;
		}

		.card-panel-icon {
			float: left;
			font-size: 48px;
		}

		.card-panel-description {
			float: right;
			font-weight: bold;
			margin: 26px;
			margin-left: 0px;
			text-align: right;

			.card-panel-text {
				line-height: 18px;
				color: rgba(0, 0, 0, 0.45);
				font-size: 16px;
				margin-top: 12px;
			}

			.card-panel-num {
				font-size: 20px;
			}
		}
	}
}

.ct-rank-bd .ct-rank-item--light {
	border-bottom: 1px solid #f5f5f5;
}

.ct-rank-item {
	padding: 24px 0;
	border-radius: 2px;
}

.ct-rank-item__l {
	border-radius: 2px;
	float: left;
}

.ct-rank-item__r {
	margin-left: 32px;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	word-wrap: normal;
}

li {
	list-style-type: none;
}

.o-ct-empty__label,
.o-ct-item__label {
	width: 20px;
	height: 20px;
	line-height: 20px;
	text-align: center;
	border-radius: 2px;
}

.o-ct-empty__label {
	background: #f5f5f5;
}

.ct-rank-item__label {
	height: 20px;
	width: 20px;
	margin-top: 11px;
	font-size: 12px;
	color: #fff;
	line-height: 20px;
	text-align: center;
	background: #c1c1c1;
	border: none;
	border-radius: 2px;
}

.pull-left {
	float: left !important;
}

.mlm {
	margin-left: 10px !important;
}

.o-ct-empty_date,
.o-ct-empty_name {
	height: 10px;
	background: #f5f5f5;
	cursor: pointer;
}

.o-ct-empty_name {
	width: 60px;
	margin: 8px 0;
}

.o-ct-empty_date {
	width: 40px;
}

.o-ct-empty_date,
.o-ct-empty_name {
	height: 10px;
	background: #f5f5f5;
	cursor: pointer;
}

.js-user-card {
	display: inline-block;
}

.fl {
	float: left;
}

.o-ct-empty-card {
	width: 40px;
	height: 40px;
	border-radius: 50%;
	background: #f5f5f5;
}

.ct-rank-item__img-icon {
	height: 30px;
	width: 22px;
	margin-top: 5px;
}

.ct-rank-item_post {
	padding-top: 4px;
	max-width: 100px;
	font-size: 12px;
	color: #909399;
	line-height: 16px;
	cursor: pointer;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	word-wrap: normal;
}

.ct-rank-item_name {
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	word-wrap: normal;
}

.ct-rank-item_name {
	max-width: 90px;
	font-size: 14px;
	color: #303133;
	line-height: 20px;
}

.ct-rank-item__img {
	display: block;
	width: 40px;
	height: 40px;
	border-radius: 10000000px;
	border-radius: 10000px;
}

.ct-rank-item__date {
	font-size: 12px;
	color: #606266;
	text-align: right;
	line-height: 16px;
	margin-top: 14px;
}

.actived {
	color: #399afb;
	border-color: #399afb;
	color: #fff;
	background-color: #399afb;
}
</style>
